package com.dream.xiaobo.controller;

import com.dream.xiaobo.entity.Address;
import com.dream.xiaobo.entity.User;
import com.dream.xiaobo.service.impl.AddressServiceImpl;
import com.dream.xiaobo.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author xiaobo
 * @date 2021/9/2 - 7:55
 */
@WebServlet("/address")
public class AddressController extends BaseServlet{

    //创建地址逻辑实例化对象
    private AddressServiceImpl addressService = new AddressServiceImpl();

    /**
     * 显示地址模块
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //获取Session中用户信息
        User user = (User) request.getSession().getAttribute("loginUser");

        //判断是否用户是否登录
        if (user == null) {
            //返回提示信息
            request.setAttribute("msg","请首先您登陆！！");
            //返回到登录页面
            return Constants.FOR_WARD + "login.jsp";
        }

        //获取用户的Id
        Integer uId = user.getuId();
        //调用查询用户的地址信息
        List<Address> list = addressService.findAddressByUId(uId);

        //将查询结果返回到前端
        request.setAttribute("list",list);

        //返回到地址详情页面
        return Constants.FOR_WARD + "/self_info.jsp";
    }

    /**
     * 添加地址模块
     * @param request
     * @param response
     * @return
     */
    public String add(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {

        //获取前端输入的内容
        Map<String, String[]> map = request.getParameterMap();
        //实例化地址对象
        Address address = new Address();

        //将实例化对象将数据库字段映射
        BeanUtils.populate(address,map);

        //调用业务逻辑方法执行
        addressService.insertAddress(address);

        //返回地址页面
        return Constants.FOR_WARD + "/address?method=show";
    }

    /**
     * 删除地址模块
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    public String delete(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {

        //获取到地址id
        String aId = request.getParameter("aid");

        //调用删除地址业务逻辑方法
        addressService.deleteAddress(aId);

        //返回地址页面
        return Constants.FOR_WARD + "/address?method=show";
    }

    /**
     * 设置默认地址模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String setDefault(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        //获取到地址id
        String aId = request.getParameter("aid");
        //调用设置默认地址业务逻辑方法
        addressService.setDefault(aId);
        //返回地址页面
        return Constants.FOR_WARD + "/address?method=show";
    }

    /**
     * 更新地址模块
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    public String update(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {

        //获取到前端输入
        Map<String, String[]> map = request.getParameterMap();
        //创建地址实例化对象
        Address address = new Address();

        //与数据库字段映射
        BeanUtils.populate(address,map);

        //调用更新地址业务逻辑方法
        addressService.updateAddress(address);

        //返回地址页面
        return Constants.FOR_WARD + "/address?method=show";
    }
}
